package cn.alm.allinoneboot.service;

import cn.alm.allinoneboot.common.CommonResult;
import cn.alm.allinoneboot.entity.Brand;
import cn.alm.allinoneboot.entity.Category;
import cn.alm.allinoneboot.entity.Product;
import cn.alm.allinoneboot.mapper.BrandMapper;
import cn.alm.allinoneboot.mapper.CategoryMapper;
import cn.alm.allinoneboot.mapper.ProductMapper;
import cn.alm.allinoneboot.utils.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

@Service
public class ProductService {

    private static List<String> categoryArr = new ArrayList<String>();

    @Resource
    private ProductMapper productMapper;

    @Resource
    private BrandMapper brandMapper;

    @Resource
    private CategoryMapper categoryMapper;

    public CommonResult addProduct(Product product) {
        String uuid = UUID.randomUUID().toString();
        product.setId(uuid);
        String currentDateWithFormat = DateUtil.getCurrentDateWithFormat();
        product.setCreateTime(currentDateWithFormat);
        product.setUpdateTime(currentDateWithFormat);
        int insert = productMapper.insert(product);
        CommonResult cr = new CommonResult();
        if (insert > 0) {
            cr.ok();
        } else {
            cr.error();
        }
        return cr;
    }

    public CommonResult updateProduct(Product product) {
        String currentDateWithFormat = DateUtil.getCurrentDateWithFormat();
        product.setUpdateTime(currentDateWithFormat);
        int update = productMapper.updateById(product);
        CommonResult cr = new CommonResult();
        if (update > 0) {
            cr.ok();
        } else {
            cr.error();
        }
        return cr;
    }

    public CommonResult deleteProductById(Product product) {
        int delete = productMapper.deleteById(product.getId());
        CommonResult cr = new CommonResult();
        if (delete > 0) {
            cr.ok();
        } else {
            cr.error();
        }
        return cr;
    }

    public CommonResult getProductById(String id) {
        Product product = productMapper.selectById(id);
        CommonResult cr = new CommonResult();
        if (product != null) {
            cr.ok();
            cr.setData(product);
        } else {
            cr.error();
        }
        return cr;
    }

    public CommonResult getProductListPage(String name, Integer current, Integer size) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(name)) {
            wrapper.like("name", name);
        }
        wrapper.orderByDesc("update_time");
        Page<Product> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        productMapper.selectPage(page, wrapper);
        List<Product> records = page.getRecords();
        for (Product product : records) {
            this.getBrandName(product);
            ProductService.categoryArr = new ArrayList<String>();
            this.getCategoryName(product.getCategoryId());
            Collections.reverse(ProductService.categoryArr);
            product.setCategoryName(String.join(" / ", ProductService.categoryArr));
        }
        CommonResult cr = new CommonResult();
        cr.ok();
        cr.setData(page);
        return cr;
    }



    public List<Product> getAllProductList() {
        List<Product> productList = productMapper.selectList(null);
        for (Product product : productList) {
            this.getBrandName(product);
            ProductService.categoryArr = new ArrayList<String>();
            this.getCategoryName(product.getCategoryId());
            Collections.reverse(ProductService.categoryArr);
            product.setCategoryName(String.join(" / ", ProductService.categoryArr));
        }
        return productList;
    }

    private void getBrandName(Product product) {
        String brandId = product.getBrandId();
        if (StringUtils.hasText(brandId)) {
            Brand brand = brandMapper.selectById(brandId);
            String name = brand.getName();
            product.setBrandName(name);
        }
    }

    private void getCategoryName(String categoryId) {
        if (categoryId.equals("0")) {
            // 根目录，无需再查询

        } else {
            Category category = categoryMapper.selectById(categoryId);
            String name = category.getName();
            ProductService.categoryArr.add(name);
            String parentId = category.getParentId();
            this.getCategoryName(parentId);
        }

    }
}
